/**********************************************************************************
**
** Copyright (C) 2009 Jostein Bratlie
** Contact: jostein.bratlie@gmail.com | http://bratlie.info | http://episteme.hin.no
**
** This file is part of GMQtExt, a GMlib and Qt extension library
**
** GMQtExt is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** GMQtExt is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with GMQtExt.  If not, see <http://www.gnu.org/licenses/>.
**
**********************************************************************************/


// Qt
#include <QDebug>
#include <QTreeView>
#include <QVBoxLayout>

// GMlib
#include <gmScene>
#include <gmSceneObject>

// local
#include "sceneitem.h"
#include "scenemodel.h"

// header
#include "scenebrowser.h"

namespace GMQtExt {

  SceneBrowser::SceneBrowser( QWidget* parent ) : QWidget( parent ) {

    QVBoxLayout *layout;
    layout = new QVBoxLayout( this );


    _model = new SceneModel( tr("Scene") );

    _view = new QTreeView;
    _view->setModel( _model );
    _view->setHeaderHidden( true );
    _view->setAnimated( true );

    layout->addWidget( _view );


    connect( _view, SIGNAL(clicked(QModelIndex)), this, SLOT(_slotItemClicked(QModelIndex)) );
  }

  void SceneBrowser::_slotItemClicked( QModelIndex idx ) {

    SceneItem *item = static_cast<SceneItem*>( _model->itemFromIndex(idx) );

    if( item )
      emit signItemClicked( item->getName() );
  }

  unsigned int SceneBrowser::getCurrentItemName() const {

    SceneItem* item = static_cast<SceneItem*>( _model->itemFromIndex( _view->currentIndex() ) );

    return ( item ? item->getName() : 0 );
  }

  void SceneBrowser::set( GMlib::Scene *scene ) {

    if( !scene )
      return;

    _model->set( scene );

    GMlib::SceneObject *active_obj = scene->getActiveObject();
    if( !active_obj )
      return;

//    _view->selectionModel()->select( _model->indexFromItem( ) );

    QList<QStandardItem*> items = _model->findItems(
      QString( "%1 [%2]").arg( active_obj->getIdentity().c_str() ).arg( active_obj->getName() ),
      Qt::MatchRecursive
    );

    foreach( QStandardItem* item, items ) {

      SceneItem *si = dynamic_cast<SceneItem*>( item );
      if( si )
        if( si->getName() == active_obj->getName() )
          _view->selectionModel()->select( _model->indexFromItem( item ), QItemSelectionModel::ClearAndSelect );
    }



  }

} // END namespace GMQtExt
